#include "mainwindowfleettest.h"
#include "ui_mainwindowfleettest.h"
#include <QTcpSocket>
//#include <QJsonDocument>
//#include <QJsonObject>
//#include <QJsonArray>
#include <QMessageBox>
#include <QThread>

#define IF_DEBUG 1

MainWindowFleetTest::MainWindowFleetTest(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    clientSocket=NULL;
    protocolCodeing=new ProtocolCodeing;
    timer.start( 1000);
    QObject::connect(&timer, SIGNAL( timeout() ), this, SLOT(timeOut_slot()));
}

MainWindowFleetTest::~MainWindowFleetTest()
{
    delete ui;
}

int MainWindowFleetTest::connectServer(QString ip, int port)
{
    if(NULL!=clientSocket)
    {
        clientSocket->disconnect();
        delete clientSocket;
    }
    clientSocket=new QTcpSocket;
    clientSocket->abort();
    clientSocket->connectToHost(ip,port);
    int waitTime=1000;//太长会卡住界面和网页访问
    if(!clientSocket->waitForConnected(waitTime))
    {
        // 登录失败
        qDebug() << "connectServer 登录失败:" << clientSocket->errorString();
        return -1;
    }
    return 1;
}

int MainWindowFleetTest::disconnectServer()
{
    clientSocket->disconnectFromHost();
}

int MainWindowFleetTest::displayAgvBasicInfo()
{

}

int MainWindowFleetTest::sendAgvCommand(QByteArray agvCommand)
{
    ui->textEdit_sendStrings->clear();
    ui->textEdit_sendStrings->setText(agvCommand);
    if(NULL==clientSocket)
    {
        return -2;
    }
    QByteArray tmpString=agvCommand;
//    tmpString+="\r\n";
//    qDebug()<<"sendGetAllInfoCommand tmpString="<<tmpString;

    //socket发送
    if(clientSocket->state() != QTcpSocket::ConnectedState)
    {
        qDebug() << " error, socket 未连接:" << clientSocket->errorString();
        return -1;
    }
    clientSocket->write(tmpString);
    clientSocket->flush();
    return 1;
}

int MainWindowFleetTest::receiveSocketBuffer(QByteArray &bufferOut)
{
    if(NULL==clientSocket)
    {
        return -2;
    }
    //socket接收
    if(clientSocket->state() != QTcpSocket::ConnectedState)
    {
        qDebug()<<"ReceiveBuff error,clientSocket->isValid()==false ";
        return -1;
    }
    QByteArray receiveByteArry = clientSocket->readAll();
    clientSocket->flush();
    if(0==receiveByteArry.size())
    {
        return 0;
    }
    bufferOut=receiveByteArry;
    return 1;
}

int MainWindowFleetTest::waitReply(int timeOut)
{
    int sleepTime=10;
    int waitTime=0;
    while(waitTime<timeOut)
    {
        QByteArray bufferOut;
        receiveSocketBuffer(bufferOut);
        int resultKey;
        resultKey=protocolCodeing->isCommandReplyOk(bufferOut);
        if(1==resultKey)
        {
            QMessageBox::information(NULL, "info", "返回成功", QMessageBox::Yes );
            return 1;
        }
        else if(-1==resultKey)
        {
            QMessageBox::information(NULL, "info", "返回失败", QMessageBox::Yes );
            return -1;
        }
        // 处理qt事件
        QCoreApplication::processEvents();//不加则接收不到新数据
        QThread::msleep(sleepTime);
        waitTime+=sleepTime;
    }
    QMessageBox::information(NULL, "info", "返回超时", QMessageBox::Yes );
    return -2;

}

void MainWindowFleetTest::timeOut_slot()
{

    if(false==ui->checkBox_isAutoReceive->isChecked())
    {
        return ;
    }
    receiveProcess();

}

void MainWindowFleetTest::on_pushButton_clicked()
{
    QString ip=ui->lineEdit_serverIp->text();
    int port=ui->lineEdit_port->text().toInt();
    if(1==connectServer(ip,port))
    {
        ui->lineEdit_connectState->setText("connected");
        QMessageBox::information(NULL, "info", "成功", QMessageBox::Yes );
    }
    else
    {
        ui->lineEdit_connectState->setText("disconnected");
        QMessageBox::information(NULL, "info", "套接字创建失败", QMessageBox::Yes );
    }
}

void MainWindowFleetTest::on_pushButton_disconnect_clicked()
{
    disconnectServer();
    ui->lineEdit_connectState->setText("disconnected");
    QMessageBox::information(NULL, "info", "已经断开", QMessageBox::Yes );
}

void MainWindowFleetTest::on_pushButton_send_clicked()
{
    QString tmpCommand="\n";
    tmpCommand+=ui->textEdit_sendText->toPlainText();
    tmpCommand+="\r";
    if(1==sendAgvCommand(tmpCommand.toLatin1()))
    {
         QMessageBox::information(NULL, "info", "send sucess", QMessageBox::Yes );
    }
    else
    {
         QMessageBox::information(NULL, "info", "send failed", QMessageBox::Yes );
    }
}

void MainWindowFleetTest::on_pushButton_receiveOnce_clicked()
{
    QByteArray tmpBuffer;
    int tmpKey=receiveSocketBuffer(tmpBuffer);
    if(0==tmpKey)
    {
        QMessageBox::information(NULL, "info", "no data received", QMessageBox::Yes );
    }
    else if(-1==tmpKey)
    {
        QMessageBox::information(NULL, "info", "no socket created", QMessageBox::Yes );
    }
    else if(-2==tmpKey)
    {
        QMessageBox::information(NULL, "info", "socket disconnected", QMessageBox::Yes );
    }
    else if(1==tmpKey)
    {
        ui->textEdit_receiveText->setText(tmpBuffer);
        processBasicInfo(tmpBuffer);
        QMessageBox::information(NULL, "info", "receive sucess", QMessageBox::Yes );
    }
    else
    {
        QMessageBox::information(NULL, "info", "unknown error", QMessageBox::Yes );
    }

}

void MainWindowFleetTest::on_pushButton_clearText_clicked()
{
    ui->textEdit_receiveText->setText("");
}

int MainWindowFleetTest::processBasicInfo(QByteArray tmpBufferIn)
{
    FleetAgvBasicInfo tmpInfo;
    if(1==protocolCodeing->parseBasicInformation(tmpBufferIn,tmpInfo))
    {
        ui->lineEdit_angularSpeed->setText(QString::number(tmpInfo.rotateSpeed));
        ui->lineEdit_lineSpeed->setText(QString::number(tmpInfo.lineSpeed));
        ui->lineEdit_pose_x->setText(QString::number(tmpInfo.position.x));
        ui->lineEdit_pose_y->setText(QString::number(tmpInfo.position.y));
        ui->lineEdit_pose_r->setText(QString::number(tmpInfo.position.rotate));
        ui->lineEdit_batteryRatio->setText(QString::number(tmpInfo.batteryLeftLightModeRatio));
        ui->lineEdit_targetLeft_line->setText(QString::number(tmpInfo.targetPositionLeft_line));
        ui->lineEdit_targetLeft_r->setText(QString::number(tmpInfo.targetPositionLeft_angle));
        ui->lineEdit_heartBeat->setText(QString::number(tmpInfo.heartBeat));
        ui->lineEdit_messageErrorId->setText(QString::number(tmpInfo.errorId));
        ui->lineEdit_messageWarnId->setText(QString::number(tmpInfo.warnId));
        ui->lineEdit_messageRemindId->setText(QString::number(tmpInfo.noticeId));
        if(true==tmpInfo.isLoaded)
        {
            ui->checkBox_isLoaded->setChecked(true);
        }
        else
        {
             ui->checkBox_isLoaded->setChecked(false);
        }
        switch(tmpInfo.state)
        {
        case E_AGV_STATE_IDILE:
        {
            ui->lineEdit_basicStatus->setText("空闲");
            break;
        }
        case E_AGV_STATE_MOVING:
        {
            ui->lineEdit_basicStatus->setText("运行中");
            break;
        }
        case E_AGV_STATE_LOSE_POSITION:
        {
            ui->lineEdit_basicStatus->setText("丢失位置");
            break;
        }
        case E_AGV_STATE_LASER_CEASE:
        {
            ui->lineEdit_basicStatus->setText("激光壁障");
            break;
        }
        case E_AGV_STATE_LASER_STOP:
        {
            ui->lineEdit_basicStatus->setText("激光停止");
            break;
        }
        case E_AGV_STATE_INFRARED_STOP:
        {
            ui->lineEdit_basicStatus->setText("红外停止");
            break;
        }
        case E_AGV_STATE_ULTRASONIC_STOP:
        {
            ui->lineEdit_basicStatus->setText("超声波停止");
            break;
        }
        case E_AGV_STATE_CHARGING:
        {
            ui->lineEdit_basicStatus->setText("充电");
            break;
        }
        case E_AGV_STATE_CEASE:
        {
            ui->lineEdit_basicStatus->setText("暂停");
            break;
        }
        case E_AGV_STATE_EMERGENCE_STOP:
        {
            ui->lineEdit_basicStatus->setText("急停");
            break;
        }
        case E_AGV_STATE_ULTRASONIC_CEASE:
        {
            ui->lineEdit_basicStatus->setText("超声波壁障");
            break;
        }
        case E_AGV_STATE_INTERNAL_ERROR:
        {
            ui->lineEdit_basicStatus->setText("内部错误");
            break;
        }
        default:
        {
             ui->lineEdit_basicStatus->setText("未知参数");
        }

        }
    }
    else
    {
        qDebug()<<"no valide data";
    }

}

int MainWindowFleetTest::processMessageInfo(QByteArray tmpBufferIn)
{
    QString msgOut;
    if(1==protocolCodeing->parseMessageInformation(tmpBufferIn,msgOut))
    {
        ui->textEdit_message->setText(msgOut);

    }
    else
    {
        qDebug()<<"no valide msg data";
    }

}

int MainWindowFleetTest::receiveProcess()
{
    QByteArray tmpBuffer;
    int tmpKey=receiveSocketBuffer(tmpBuffer);
    if(0==tmpKey)
    {
//        QMessageBox::information(NULL, "info", "no data received", QMessageBox::Yes );
    }
    else if(-1==tmpKey)
    {
//        QMessageBox::information(NULL, "info", "no socket created", QMessageBox::Yes );
    }
    else if(-2==tmpKey)
    {
//        QMessageBox::information(NULL, "info", "socket disconnected", QMessageBox::Yes );
    }
    else if(1==tmpKey)
    {
        ui->textEdit_receiveText->setText(tmpBuffer);
        processBasicInfo(tmpBuffer);
        processMessageInfo(tmpBuffer);
//        QMessageBox::information(NULL, "info", "receive sucess", QMessageBox::Yes );
    }
    else
    {
//        QMessageBox::information(NULL, "info", "unknown error", QMessageBox::Yes );
    }

}

void MainWindowFleetTest::on_pushButton_clearAllPath_clicked()
{
    QString tmpCommand="\nDE\r";
    if(1==sendAgvCommand(tmpCommand.toLatin1()))
    {
        //todo 等待控制器回应，超时代表命令丢失了。
        //成功返回字符串QString tmpCommand="\nOK\r";
        //失败返回字符串QString tmpCommand="\nBD\r";
        waitReply(3500);
    }
    else
    {
        QMessageBox::information(NULL, "info", "命令发送失败", QMessageBox::Yes );
    }
}

void MainWindowFleetTest::on_pushButton_addPath_clicked()
{
    //组帧
    QString tmp_frame;
    QString tmp_directin;
    if(0==ui->comboBox_direction->currentIndex())
    {
        tmp_directin='S';
    }
    else
    {
        tmp_directin='N';
    }
    QString tmp_translation;
    if(0==ui->comboBox_translation->currentIndex())
    {
        tmp_translation='M';
    }
    else
    {
        tmp_translation='T';
    }
    QString tmp_attribute;
    if(0==ui->comboBox_attribute->currentIndex())
    {
        tmp_attribute='Q';
    }
    else if(1==ui->comboBox_attribute->currentIndex())
    {
        tmp_attribute='A';
    }


    QString xPos,yPos,rPos;
    xPos=ui->lineEdit_poseCommand_x->text();
    yPos=ui->lineEdit_poseCommand_y->text();
    rPos=ui->lineEdit_poseCommand_r->text();

    tmp_frame='\n';
    tmp_frame+="TG";
    tmp_frame+="#"+tmp_directin;
    tmp_frame+="#"+tmp_translation;
    tmp_frame+="#"+tmp_attribute;
    tmp_frame+=":"+xPos;
    tmp_frame+=","+yPos;
    tmp_frame+=","+rPos;
    tmp_frame+='\r';
    if(1==sendAgvCommand(tmp_frame.toLatin1()))
    {
        //todo 等待控制器回应，超时代表命令丢失了。
        //成功返回字符串QString tmpCommand="\nOK\r";
        //失败返回字符串QString tmpCommand="\nBD\r";
        waitReply(3500);
    }
    else
    {
        QMessageBox::information(NULL, "info", "命令发送失败", QMessageBox::Yes );
    }
}

void MainWindowFleetTest::on_pushButton_setForkHeight_clicked()
{
    //组帧
    QString tmp_frame;
    QString tmp_startHeight,tmp_endHeight;
    tmp_startHeight=ui->lineEdit_forkStartHeight->text();
    tmp_endHeight=ui->lineEdit_forkEndHeight->text();

    tmp_frame='\n';
    tmp_frame+="HT";
    tmp_frame+="#"+tmp_startHeight;
    tmp_frame+=";"+tmp_endHeight;
    tmp_frame+='\r';
    if(1==sendAgvCommand(tmp_frame.toLatin1()))
    {
        //todo 等待控制器回应，超时代表命令丢失了。
        //成功返回字符串QString tmpCommand="\nOK\r";
        //失败返回字符串QString tmpCommand="\nBD\r";
        waitReply(3500);
    }
    else
    {
        QMessageBox::information(NULL, "info", "命令发送失败", QMessageBox::Yes );
    }
}

void MainWindowFleetTest::on_pushButton_pause_clicked()
{
    QString tmpCommand="\nPA\r";
    if(1==sendAgvCommand(tmpCommand.toLatin1()))
    {
        //todo 等待控制器回应，超时代表命令丢失了。
        //成功返回字符串QString tmpCommand="\nOK\r";
        //失败返回字符串QString tmpCommand="\nBD\r";
        waitReply(3500);
    }
    else
    {
        QMessageBox::information(NULL, "info", "命令发送失败", QMessageBox::Yes );
    }
}

void MainWindowFleetTest::on_pushButton_stop_clicked()
{
    QString tmpCommand="\nST\r";
    if(1==sendAgvCommand(tmpCommand.toLatin1()))
    {
        //todo 等待控制器回应，超时代表命令丢失了。
        //成功返回字符串QString tmpCommand="\nOK\r";
        //失败返回字符串QString tmpCommand="\nBD\r";
        waitReply(3500);
    }
    else
    {
        QMessageBox::information(NULL, "info", "命令发送失败", QMessageBox::Yes );
    }
}

void MainWindowFleetTest::on_pushButton_start_clicked()
{
    QString tmpCommand="\nGO\r";
    if(1==sendAgvCommand(tmpCommand.toLatin1()))
    {
        //todo 等待控制器回应，超时代表命令丢失了。
        //成功返回字符串QString tmpCommand="\nOK\r";
        //失败返回字符串QString tmpCommand="\nBD\r";
        waitReply(3500);
    }
    else
    {
        QMessageBox::information(NULL, "info", "命令发送失败", QMessageBox::Yes );
    }
}



void MainWindowFleetTest::on_pushButton_clearMessage_clicked()
{
    QString tmpCommand="\nCLEARMSG\r";
    if(1==sendAgvCommand(tmpCommand.toLatin1()))
    {
        //todo 等待控制器回应，超时代表命令丢失了。
        //成功返回字符串QString tmpCommand="\nOK\r";
        //失败返回字符串QString tmpCommand="\nBD\r";
        waitReply(3500);
    }
    else
    {
        QMessageBox::information(NULL, "info", "命令发送失败", QMessageBox::Yes );
    }
}

void MainWindowFleetTest::on_pushButton_addMusic_clicked()
{
    //组帧
    QString tmp_frame;
    QString musicName;
    musicName=ui->lineEdit_musicName->text();
    QString playTimes=ui->lineEdit_musicPlayTimes->text();
    QString volume=ui->lineEdit_musicPlayVolume->text();

    tmp_frame='\n';
    tmp_frame+="MUSIC";
    tmp_frame+="#"+musicName;
    tmp_frame+="#"+volume;
    tmp_frame+="#"+playTimes;
    tmp_frame+='\r';
    if(1==sendAgvCommand(tmp_frame.toLatin1()))
    {
        //todo 等待控制器回应，超时代表命令丢失了。
        //成功返回字符串QString tmpCommand="\nOK\r";
        //失败返回字符串QString tmpCommand="\nBD\r";
        waitReply(3500);
    }
    else
    {
        QMessageBox::information(NULL, "info", "命令发送失败", QMessageBox::Yes );
    }
}

void MainWindowFleetTest::on_pushButton_addMusic_2_clicked()
{
    //组帧
    QString tmp_frame;
    QString mapName;
    mapName=ui->lineEdit_mapName->text();
    QString x=ui->lineEdit_initialX->text();
    QString y=ui->lineEdit_initialY->text();
    QString r=ui->lineEdit_initialR->text();

    tmp_frame='\n';
    tmp_frame+="MAP";
    tmp_frame+="#"+mapName;
    tmp_frame+="#"+x;
    tmp_frame+="#"+y;
    tmp_frame+="#"+r;
    tmp_frame+='\r';
    if(1==sendAgvCommand(tmp_frame.toLatin1()))
    {
        //todo 等待控制器回应，超时代表命令丢失了。
        //成功返回字符串QString tmpCommand="\nOK\r";
        //失败返回字符串QString tmpCommand="\nBD\r";
        waitReply(3500);
    }
    else
    {
        QMessageBox::information(NULL, "info", "命令发送失败", QMessageBox::Yes );
    }
}

void MainWindowFleetTest::on_pushButton_setVel_clicked()
{
    //组帧
    QString tmp_frame;
    QString vel;
    vel=ui->lineEdit_setVelRatio->text();


    tmp_frame='\n';
    tmp_frame+="SETVEL";
    tmp_frame+="#"+vel;
    tmp_frame+='\r';
    if(1==sendAgvCommand(tmp_frame.toLatin1()))
    {
        //todo 等待控制器回应，超时代表命令丢失了。
        //成功返回字符串QString tmpCommand="\nOK\r";
        //失败返回字符串QString tmpCommand="\nBD\r";
        waitReply(3500);
    }
    else
    {
        QMessageBox::information(NULL, "info", "命令发送失败", QMessageBox::Yes );
    }
}


void MainWindowFleetTest::on_pushButton_setCharge_clicked()
{
    //组帧
    QString tmp_frame;
    QString chargeMethod;
    if(0==ui->comboBox_chargeMethod->currentIndex())
    {
        chargeMethod='T';
    }
    else if(1==ui->comboBox_chargeMethod->currentIndex())
    {
        chargeMethod='V';
    }
    else
    {
        chargeMethod='N';
    }
    QString chargeVolume=ui->lineEdit_chargeVolume->text();
    QString chargeTime=ui->lineEdit_chargeTime->text();

    tmp_frame='\n';
    tmp_frame+="CHARGE";
    tmp_frame+="#"+chargeMethod;
    tmp_frame+="#"+chargeTime;
    tmp_frame+="#"+chargeVolume;
    tmp_frame+='\r';
    if(1==sendAgvCommand(tmp_frame.toLatin1()))
    {
        //todo 等待控制器回应，超时代表命令丢失了。
        //成功返回字符串QString tmpCommand="\nOK\r";
        //失败返回字符串QString tmpCommand="\nBD\r";
        waitReply(3500);
    }
    else
    {
        QMessageBox::information(NULL, "info", "命令发送失败", QMessageBox::Yes );
    }
}

void MainWindowFleetTest::on_pushButton_setLift_clicked()
{
    //组帧
    QString tmp_frame;
    QString mechanicalType;
    if(0==ui->MechanismSelete_ComBox->currentIndex())
    {
        mechanicalType='A';
    }
    else if(1==ui->MechanismSelete_ComBox->currentIndex())
    {
        mechanicalType='B';
    }
    QString moveType;
    if(0==ui->MechanismSelete_ComBox_riseOrDown->currentIndex())
    {
        moveType='U';
    }
    else if(1==ui->MechanismSelete_ComBox_riseOrDown->currentIndex())
    {
        moveType='D';
    }
    QString timeOut=ui->TimeOut_Value_Edit->text();

    tmp_frame='\n';
    tmp_frame+="LIFT";
    tmp_frame+="#"+mechanicalType;
    tmp_frame+="#"+moveType;
    tmp_frame+="#"+timeOut;
    tmp_frame+='\r';
    if(1==sendAgvCommand(tmp_frame.toLatin1()))
    {
        //todo 等待控制器回应，超时代表命令丢失了。
        //成功返回字符串QString tmpCommand="\nOK\r";
        //失败返回字符串QString tmpCommand="\nBD\r";
        waitReply(3500);
    }
    else
    {
        QMessageBox::information(NULL, "info", "命令发送失败", QMessageBox::Yes );
    }
}

void MainWindowFleetTest::on_pushButton_setRoller_clicked()
{
    //组帧
    QString tmp_frame;
    QString mechanicalType;
    if(0==ui->MechanismSelete_ComBox_2->currentIndex())
    {
        mechanicalType='A';
    }
    else if(1==ui->MechanismSelete_ComBox_2->currentIndex())
    {
        mechanicalType='B';
    }
    QString moveType;
    if(0==ui->MechanismSelete_ComBox_riseOrDown_2->currentIndex())
    {
        moveType='L';
    }
    else if(1==ui->MechanismSelete_ComBox_riseOrDown_2->currentIndex())
    {
        moveType='R';
    }
    QString timeOut=ui->TimeOut_Value_Edit_2->text();

    tmp_frame='\n';
    tmp_frame+="ROLLER";
    tmp_frame+="#"+mechanicalType;
    tmp_frame+="#"+moveType;
    tmp_frame+="#"+timeOut;
    tmp_frame+='\r';
    if(1==sendAgvCommand(tmp_frame.toLatin1()))
    {
        //todo 等待控制器回应，超时代表命令丢失了。
        //成功返回字符串QString tmpCommand="\nOK\r";
        //失败返回字符串QString tmpCommand="\nBD\r";
        waitReply(3500);
    }
    else
    {
        QMessageBox::information(NULL, "info", "命令发送失败", QMessageBox::Yes );
    }
}

void MainWindowFleetTest::on_pushButton_addPath_complex_clicked()
{
    //组帧
    QString tmp_frame;
    QString tmp_directin;
    if(0==ui->comboBox_direction->currentIndex())
    {
        tmp_directin='S';
    }
    else
    {
        tmp_directin='N';
    }
    QString tmp_translation;
    if(0==ui->comboBox_translation->currentIndex())
    {
        tmp_translation='M';
    }
    else
    {
        tmp_translation='T';
    }
    QString tmp_attribute;
    if(0==ui->comboBox_attribute->currentIndex())
    {
        tmp_attribute='Q';
    }
    else if(1==ui->comboBox_attribute->currentIndex())
    {
        tmp_attribute='A';
    }


    QString xPos,yPos,rPos;
    xPos=ui->lineEdit_poseCommand_x->text();
    yPos=ui->lineEdit_poseCommand_y->text();
    rPos=ui->lineEdit_poseCommand_r->text();

    QString isDetect;
    if(true==ui->checkBox_isDetectBarrier->isChecked())
    {

        isDetect="DE";
        isDetect+=QString::number(ui->comboBox_detectBarrierMode->currentIndex());
    }
    else
    {
        isDetect="NODE";
    }
    QString stopAccuracy=ui->lineEdit_stopAccuracy->text();
    QString vel=ui->lineEdit_command_vel->text();
    QString pointType;
    int typeIndex=ui->comboBox_positionType->currentIndex();
    switch(typeIndex)
    {
    case 0:
    {
        pointType="AM";
        break;
    }
    case 1:
    {
        pointType="VMARK";
        break;
    }
    case 2:
    {
        pointType="REFLECT";
        break;
    }
    case 3:
    {
        pointType="QR";
        break;
    }
       default:
    {

    }
    }

    QString pointTypeIndex=ui->lineEdit_positionTypeIndex->text();
    QString laserFilterIndex=ui->lineEdit_laserFilterType->text();

    QString pathType;
    if(0==ui->comboBox_pathType->currentIndex())
    {
        pathType="LINE";
    }
    else if(1==ui->comboBox_pathType->currentIndex())
    {
        pathType="ROTATE";
    }
    else if(2==ui->comboBox_pathType->currentIndex())
    {
        pathType="SPLINE";
    }
    else
    {
        qDebug()<<"error,,,,unknow type";
        return ;
    }

    tmp_frame='\n';
    tmp_frame+="POINT";
    tmp_frame+="#"+tmp_directin;
    tmp_frame+="#"+tmp_translation;
    tmp_frame+="#"+tmp_attribute;
    tmp_frame+=":"+xPos;
    tmp_frame+=","+yPos;
    tmp_frame+=","+rPos;
    tmp_frame+=","+pointType;
    tmp_frame+=","+pointTypeIndex;
    tmp_frame+=","+laserFilterIndex;
    tmp_frame+=","+vel;
    tmp_frame+=","+isDetect;
    tmp_frame+=","+stopAccuracy;
    tmp_frame+=","+pathType;
    tmp_frame+=","+QString::number(1);
    tmp_frame+=","+QString::number(1);
    tmp_frame+='\r';
    if(1==sendAgvCommand(tmp_frame.toLatin1()))
    {
        //todo 等待控制器回应，超时代表命令丢失了。
        //成功返回字符串QString tmpCommand="\nOK\r";
        //失败返回字符串QString tmpCommand="\nBD\r";
        waitReply(3500);
    }
    else
    {
        QMessageBox::information(NULL, "info", "命令发送失败", QMessageBox::Yes );
    }
}

void MainWindowFleetTest::on_pushButton_calculateValue_clicked()
{
    bool isOk;
    unsigned int tmpValue=ui->lineEdit_test_value_buma->text().toUInt(&isOk,16);
    ui->lineEdit_test_value->setText(QString::number((int)tmpValue));
}

void MainWindowFleetTest::on_pushButton_calculateBuma_clicked()
{
    int tmpValue=ui->lineEdit_test_value->text().toInt();
    unsigned tmpUn=tmpValue;
    QString tmp;
    tmp.sprintf("%02x",tmpUn);
    ui->lineEdit_test_value_buma->setText(tmp);

}

void MainWindowFleetTest::on_pushButton_addRobotArmCommand_clicked()
{
    //组帧
    QString tmp_frame;
    QString tmp_moveType;
    if(0==ui->comboBox_robotArmMoveType->currentIndex())
    {
        tmp_moveType='J';
    }
    else if(1==ui->comboBox_robotArmMoveType->currentIndex())
    {
        tmp_moveType='L';
    }
    else if(2==ui->comboBox_robotArmMoveType->currentIndex())
    {
        tmp_moveType='C';
    }

    QString tmp_pointType;
    if(0==ui->comboBox_robotArmMoveType->currentIndex())
    {
        tmp_pointType="JX";
    }
    else if(1==ui->comboBox_robotArmMoveType->currentIndex())
    {
        tmp_pointType="DX";
    }

    QString tmp_speed=ui->lineEdit_robotArm_speedRatio->text();

    QString tmp_axisCountStr=ui->lineEdit_robotArmAxisCount->text();
    int tmp_axisCount=tmp_axisCountStr.toInt();



    tmp_frame='\n';
    tmp_frame+="MOVEARM";
    tmp_frame+="#"+tmp_moveType;
    tmp_frame+="#"+tmp_speed;
    tmp_frame+="#"+tmp_pointType;
    tmp_frame+="#"+tmp_axisCountStr;
    if(0==ui->comboBox_robotArmMoveType->currentIndex())
    {
        if(1<=tmp_axisCount)
        {
            tmp_frame+="#"+ui->lineEdit_robotArmPoint_j1->text();
        }
        if(2<=tmp_axisCount)
        {
            tmp_frame+="#"+ui->lineEdit_robotArmPoint_j2->text();
        }
        if(3<=tmp_axisCount)
        {
            tmp_frame+="#"+ui->lineEdit_robotArmPoint_j3->text();
        }
        if(4<=tmp_axisCount)
        {
            tmp_frame+="#"+ui->lineEdit_robotArmPoint_j4->text();
        }
        if(5<=tmp_axisCount)
        {
            tmp_frame+="#"+ui->lineEdit_robotArmPoint_j5->text();
        }
        if(6<=tmp_axisCount)
        {
            tmp_frame+="#"+ui->lineEdit_robotArmPoint_j6->text();
        }
    }
    else
    {
        tmp_frame+="#"+ui->lineEdit_robotArmPoint_x->text();
        tmp_frame+="#"+ui->lineEdit_robotArmPoint_y->text();
        tmp_frame+="#"+ui->lineEdit_robotArmPoint_z->text();
        tmp_frame+="#"+ui->lineEdit_robotArmPoint_w->text();
        tmp_frame+="#"+ui->lineEdit_robotArmPoint_p->text();
        tmp_frame+="#"+ui->lineEdit_robotArmPoint_r->text();
    }

    tmp_frame+='\r';
    if(1==sendAgvCommand(tmp_frame.toLatin1()))
    {
        //todo 等待控制器回应，超时代表命令丢失了。
        //成功返回字符串QString tmpCommand="\nOK\r";
        //失败返回字符串QString tmpCommand="\nBD\r";
        waitReply(3500);
    }
    else
    {
        QMessageBox::information(NULL, "info", "命令发送失败", QMessageBox::Yes );
    }
}

void MainWindowFleetTest::on_pushButton_addAutoDrive_clicked()
{
    //组帧
    QString tmp_frame;
    QString stationId;
    stationId=ui->lineEdit_stationId_autoDrive->text();
    QString speed=ui->lineEdit_autoDriveSpeed->text();

    tmp_frame='\n';
    tmp_frame+="AUTODRIVE";
    tmp_frame+="#"+stationId;
    tmp_frame+="#"+speed;
    tmp_frame+="#"+QString::number(0);
    tmp_frame+='\r';
    if(1==sendAgvCommand(tmp_frame.toLatin1()))
    {
        //todo 等待控制器回应，超时代表命令丢失了。
        //成功返回字符串QString tmpCommand="\nOK\r";
        //失败返回字符串QString tmpCommand="\nBD\r";
        waitReply(3500);
    }
    else
    {
        QMessageBox::information(NULL, "info", "命令发送失败", QMessageBox::Yes );
    }
}
